﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Threading;
using Hammock;
using Hammock.Web;
using HtmlAgilityPack;
using _1906Ultras.Model;

namespace _1906Ultras.Helpers
{
    public class SongHelper : HelperBase
    {
        public SongHelper()
        {
            Authority = "http://www.1906ultras.com/";
            Request.Path = "songs";
        }


        public void GetSongs(Action<ObservableCollection<Song>> callback)
        {
            BeginRequest((response) =>
                             {
                                 try
                                 {
                                     HtmlDocument htmDoc = new HtmlDocument();
                                     htmDoc.LoadHtml(response);

                                     var nodes =
                                         htmDoc.DocumentNode.SelectNodes(
                                             "//div[@id='songholder']");

                                     var songs = new ObservableCollection<Song>();
                                     foreach (var node in nodes)
                                     {
                                         var title = node.ChildNodes["div"].InnerText;
                                         var lyrics = node.ChildNodes[3].InnerText;
                                         lyrics = Regex.Replace(lyrics, @"[\f\r\t\v]", "");
                                         lyrics = Regex.Replace(lyrics, " +", " ");
                                         var s = new Song()
                                                     {
                                                         Title = title.Trim(),
                                                         Lyrics = lyrics.Trim()
                                                     };
                                         songs.Add(s);

                                     }

                                     //var tweets = JsonConvert.DeserializeObject<ObservableCollection<Tweet>>(s);
                                     DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                                                               {
                                                                                   GlobalLoading.Instance.IsLoading = false;
                                                                                   callback(songs);
                                                                               });

                                 }
                                 catch (Exception e)
                                 {
                                     ErrorCallbackInternal(e);
                                 }


                             });

        }
    }
}